(function () {
    var $tbody = $("#list-body"),
    	$searchForm = $("#search-form"),
    	$feedbackDate = $("#feedbackdate"),
        trHtml = undefined,
        curPage = 1;
    var _this = {
    	// 表格初始化 
        list : function(params){
        	params = params || {};
//          params = $searchForm.serializeObject();
            params.page = curPage;
            params.size=10;
            App.ajax({
                data: params,
                resUrl: "ygxStudent/list",		// 分页查询接口
                callback: function (rb) {
                    if(!rb.status) return;
                    var rbData = rb.data;
                    $tbody.empty();
                    $.each(rbData.list||[], function(i, d){
                        var $tr = $(trHtml);
                        if( rbData.pageNumber == 1 ){
                        	d.num = i + 1;
                        }else{
                        	d.num = (rbData.pageNumber - 1) * 10 + i + 1;
                        }
                        $tr.setHtml(d).data(d);
                        $tr.find("button").data("id", d.id);
                        $tbody.append($tr);
                    });
                    require(['laypage'], function (laypage) {
                        laypage({
                            cont: 'page-body', //容器。值支持id名、原生dom对象，jquery对象。【如该容器为】：<div id="page1"></div>
                            pages:rbData.totalPage, //通过后台拿到的总页数
                            curr: rbData.pageNumber || 1, //当前页
                            jump: function (obj, first) { //触发分页后的回调
                                curPage = obj.curr;
                                if (!first) {
                                    _this.list();
                                }
                            }
                        });
                    });
                }
            });
        },
        // 事件初始化
        initEvent : function(){
        	App.delegate({
				btntype: "[btn-type=details]",
				callback: function(data) {
					if(data.status==2){  //这段是在查看详情时把这条数据从未读改为已读
						status=1;
						var postData = { id: data.id, status: status };
						App.ajax({
							data: postData,
							resUrl: "/company/status",
							callback: function (rb) {
//								if (!rb.status) {
//									layer.msg(rb.msg||'操作不成功', { icon: 5 });
//									return;
//								}
//								layer.msg('成功', { icon: 1 });
								data.status = status;
								$tr.data(data);
								_this.initStatus($tr, status);
	
							}
						});
					}
					var msg = '订单内容...........';
					App.ajax({
						data:{data1:'data1'},
						resUrl:'ygxStudent/list',
						callback:function(rb){
							msg='这是请求接口得到的内容！';
							console.log(111)
							var btnok = '前往订单！'
				        	var _index = layer.confirm('<div style="margin: auto;width: 222px;">'+msg+'</div>', {
								type:1,
								title:"查看订单",
								area: ['400px', 'auto'], //宽高
								btn: [btnok]
							}, function(){
				                    console.log(777)
							}, function(){
								// 取消
								layer.close(_index);
							});
						}
					});
		        	
				}
			});
			App.delegate({
			    btntype: "[btn-type=status]",
			    callback: function (data, $tr) {
					var status = data.status == 2 ? 1 : 2;
					var postData = { id: data.id, status: status };
					App.ajax({
						data: postData,
						resUrl: "/company/status",
						callback: function (rb) {
							if (!rb.status) {
								layer.msg(rb.msg||'操作不成功', { icon: 5 });
								return;
							}
							layer.msg('成功', { icon: 1 });
							data.status = status;
							$tr.data(data);
							_this.initStatus($tr, status);

						}
					});
				}
			});
			$("#startDate").dateRangePicker({
				autoClose: true,
				singleDate: true,
				timePicker: false,
				startOfWeek: 'Wednesday',
				format: 'YYYY-MM-DD',
				time: {
					enabled: false 
				}, 
				callback: function(){
               		if( _this.dayDiff( $("#startDate").val().split(' ')[0] , $("#endDate").val().split(' ')[0] ) <= 0 ){
               			$("#endDate").val('');
               		}
               		if( $("#startDate").val().split(' ')[0] != '' ){
	               		var startTime = $("#startDate").val().split(' ')[0];
	               		$("#endDate").dateRangePicker(
							{
								startDate: startTime,
			    				endDate: '2020-12-31',
								autoClose: false,
								singleDate: true,
								timePicker : false,
								startOfWeek: 'Wednesday',
								format:'YYYY-MM-DD',
								time: {
				                    enabled: false
				               },
				               callback: function(){
				               }
							}
						);
               		}
               	}
			});
			$("#endDate").dateRangePicker(
				{
					autoClose: true,
					singleDate: true,
					timePicker : false,
					startOfWeek: 'Wednesday',
					format:'YYYY-MM-DD',
					time: {
	                    enabled: false
	               	},
	               	callback: function(){
	                }
				}
			);
        	// 查询初始化
            $searchForm.submit(function(){
            	var params = $searchForm.serializeObject();
            	var feedbackDate = $feedbackDate.val()||"";
				if(feedbackDate.indexOf("to")!=-1){
					params.beginDt = feedbackDate.split(" to ")[0];
					params.endDt = feedbackDate.split(" to ")[1];
				}
				if(!params.beginDt || !params.endDt){
					layer.msg("请正确选择发送日期区间", { icon: 2 });
					return;
				}
				curPage = 1;
				_this.list(params);
				return false;
			});
        },
        /**
		 * 两个日期的天数差，参数格式：'YYYY-MM-DD HH:mm:ss'
		 * @param {Object} startDate 开始日期	
		 * @param {Object} endDate 结束日期
		 */
		dayDiff: function( startDate, endDate ){
			var beginTime = startDate;
			var endTime = endDate;
			var beginDate = new Date(beginTime);
			var endDate = new Date(endTime);
			var day = (endDate.getTime() - beginDate.getTime() ) / ( 24*60*60*1000 ) + 1;
			return day;
		},
        init : function () {
            trHtml = $tbody.html();
            $tbody.empty().show();
            _this.initEvent();
            _this.list();
        }
    }
    _this.init(); //页面初始化入口设置
})();
